<?php

namespace Admin\Controller;

use Admin\Logic\MemberLogic;
use Admin\Logic\TableLogic;
use Admin\Logic\UserLogic;
use function array_keys;
use function is_array;
use function json_decode;

class MemberController extends CommonController
{
    /**
     * 员工列表展示
     * @return [type] [description]
     */
    public function index()
    {

        $param = [];
        if (IS_POST) {
            $param = [
                'nickname' => I('post.nickname_query'),
            ];
        }

        $data = MemberLogic::getAll($param);

        $this->assign('data', $data);
        $this->assign('param', $param['nickname']);
        $this->display();
    }


    /**
     * 添加员工视图
     */
    public function add()
    {
        // 获取所有餐桌
        $table = TableLogic::getAll();

        $this->assign(['table' => $table]);
        $this->display();
    }


    /**
     * 处理添加管理员逻辑
     * @return [type] [description]
     */
    public function store()
    {
        $this->checkPost();
        $table_id = substr($_POST['table'], 0, -1);

        $param = [
            'nickname' => I('post.nickname', '', 'htmlspecialchars'),
            'mobile' => I('post.mobile'),
            'email' => I('post.email'),
            'sex' => I('post.sex'),
            'created_at' => getDateTime(),
            'table_id' => I('post.table'),
            'nick_path' => I('post.nick_path')
        ];
        $user = MemberLogic::create($param);
        if ($user['code'] == 1) {
            return $this->ajaxError($user['msg']);
        }
        return $this->ajaxSuccess();
    }

    /**
     * 编辑用户信息
     * @return [type] [description]
     */
    public function edit()
    {

        $id = I('get.id', 0, 'intval');

        if ($id < 1) {
            E('非法操作');
        }

        $user = D('Member')->where(['id' => $id])->find();

        $this->assign('user', $user);

        $this->display();
    }


    /**
     * 展示评论页面
     */
    public function comment()
    {
        $id = I('get.id');
        $order_id = I('get.order_id');
        $message = M('order')->where(['id' => $order_id])->field(['pinglun'])->find();
        $data = MemberLogic::getAll(['id' => $id]);

        $this->assign('message', $message);
        $this->assign('order_id', $order_id);
        $this->assign(['data' => $data[0]]);
        $this->display();
    }


    /**
     * 执行添加评论的操作
     * @return string
     */
    public function addComment()
    {
        $data = [
            'user_id' => I('post.id'),
            'order_id' => I('post.order_id'),
            'degree' => I('post.degree'),
            'content' => I('post.content'),
            'created_at' => getDateTime(),
        ];

        $result = MemberLogic::createMessage($data);

        if ($result['code'] > 0) {
            return $this->ajaxError();
        } else {
            return $this->ajaxSuccess();
        }

    }

    /**
     * 显示历史评论
     */
    public function history()
    {
        $id = I('get.id');

        $data = MemberLogic::getAllHistory($id);


        if (empty($data['data'])) {
            echo '<h3>暂无评论</h3>';
            return;
        }

        $this->assign(['data' => $data['data'], 'count' => $data['count']]);
        $this->display();


    }


    public function form()
    {
        $form = D('Member as m')
            ->join('left join food_message as me on m.id = member_id')
            ->field('m.*, sum(if(`fenshu`=3,1,0)) as 满意数量,sum(if(`fenshu`=2,1,0)) as 一般满意数量,sum(if(`fenshu`=1,1,0))as 不满意数量, me.id as m_id')
            ->group('m.id')
            ->select();
//
        $d = D('Order');
        foreach ($form as $k => $v) {
            $res = $d->where(['order_name' => ['in', $v['table_id']]])->field('id,count(id) as 服务客桌数,sum(order_total) as 服务金额')->find();
            $form[$k]['服务客桌数'] = $res['服务客桌数'];
            $form[$k]['服务金额'] = $res['服务金额'];

            $form[$k]['利润'] = ($form[$k]['服务金额']) - (MemberLogic::getMemberLoss($v['id']));
        }
        $this->assign(['member' => $form]);
        $this->display();

    }


    /**
     * 处理更新用户信息
     * @return [type] [description]
     */
    public function update()
    {
        $this->checkPost();
        $param = [
            'id' => I('post.id', 0, 'intval'),
            'nickname' => I('post.nickname', '', 'htmlspecialchars'),
            'email' => I('post.email'),
            'sex' => I('post.sex'),
            'mobile' => I('post.mobile'),
            'address' => I('post.address'),
            'update_at' => date('Y-m-d H:i:s')
        ];


        $update = MemberLogic::update($param);

        if ($update == false) {
            return $this->ajaxError('服务器找不到啦~^_^');
        }

        return $this->ajaxSuccess();

    }


    /**
     * 删除管理员用户
     * @return [type] [description]
     */
    public function delete()
    {
        $id = I('get.id', 0, 'intval');
        $data = MemberLogic::delete($id);
        if ($data > 0) {
            return $this->ajaxSuccess();
        } else {
            return $this->ajaxError('删除失败，请重试');
        }
    }

    public function table()
    {
        $table = TableLogic::getAll();
        echo json_encode($table);
    }

    public function showDetail()
    {
        $id = I('get.id');
        $message = M('message')->where(['id' => $id])->find();
        $member = M('member')->where(['id' => $message['member_id']])->find();
        $message['food_message'] = json_decode($message['food_message'], true);
        $message['member_name'] = $member['nickname'];
        if (is_array($message['food_message']) && !empty($message['food_message'])) {
            $ids = array_keys($message['food_message']);

            $food = M('food')->where(['id' => ['in', $ids]])->field(['name', 'id'])->select();
            if (!empty($food)) {
                $message['food_message'] = array_flip($message['food_message']);
                $message['new_food_message'] = array_flip($message['food_message']);
                $tmp = [];
                foreach ($food as $key => $val) {
                    if (in_array($val['id'], $message['food_message'])) {
                        $tmp[$val['name']] = $message['new_food_message'][$val['id']];
                    }
                }
                $message['pingjia'] = $tmp;
                unset($message['food_message']);
                unset($message['new_food_message']);
            }
        }
        $this->assign('message', $message);
        $this->display();
    }


}